GBML manual / Maths

abs_int

The same as abs, for a whole number: value without its sign, always zero or positive, and the answer is a whole number. Use this one whenever the number is counted rather than measured - how many slots apart two things are, how many frames either side of a moment - so the variable you keep it in stays a whole number.

Syntax

abs_int(value) -> int

Arguments

NameType
valueint

Returns

int

Description

The same as abs, for a whole number: value without its sign, always zero or positive - and the answer is a whole number. abs_int(-7) and abs_int(7) are both 7.

Reach for this one whenever the number is counted rather than measured: how many slots apart two things are, how many frames either side of a moment, how far a menu cursor has to travel. Use abs for things that are measured - a position, a speed, a distance on screen.

Example

var gap = abs_int(int_floor(x) - int_floor(xstart));

Works out how many whole pixels this object has drifted from where it spawned, whichever side it drifted to. gap is a whole number, so it can be handed to anything that wants one.

Why there are two of these

abs and abs_int do the same arithmetic. They differ in the type of the answer, and that difference is worth a function of its own because of what goes wrong without it.

abs hands back a real. So this line:

gap = abs(gap - 5);          <- the trap: this is NOT what you want here

turns gap into a real - the value is still a whole one, but the kind of number has changed. Nothing complains there. The complaint arrives later, somewhere else, wherever gap is next handed to something that wants a whole number, such as the frame of draw_sprite, which says which picture of a sprite to show:

Argument 4 to `draw_sprite` should be an int, but this is a real.

That error points at a line that looks perfectly fine, and it may be in a different file from the assignment that caused it. Written with abs_int, it cannot happen.

The number going in has to be whole

abs_int takes a whole number only. A number with a point in it is a real - even 4.0 - and is refused when the project is checked. x is a real, so this is refused:

gap = abs_int(x - 120);
Argument 1 to `abs_int` should be an int, but this is a real.

If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round, as the example above does. If the number really is measured, use abs instead.

It goes further than abs

A real runs out at about 524,000 and a whole number does not run out until about 2.1 billion, so abs_int(-600000) is 600000. There is exactly one whole number that has no positive twin, -2147483648: abs_int hands it back unchanged, still negative, because 2147483648 does not fit. Nothing a game counts gets near it.

Cost

A sign check - as cheap as this group gets, safe to call every frame.

On the GBA

abs and abs_int differ in TYPE, not in arithmetic. abs hands back a real, so `gap = abs(gap - 5)` turns a whole number into a real one, and the next place that wants a whole number refuses it - usually somewhere else entirely, which makes it a puzzle. abs_int hands back a whole number, so it cannot happen. The number going in has to be whole too: abs_int(x - 120) is refused when the project is checked because x is a real - round it first with int_floor, int_ceil or int_round. A whole number goes much further than a real: abs_int(-600000) is 600000, where a real runs out at about 524,000. The one whole number with no positive twin, -2147483648, comes back as itself. A sign check, cheap enough for every frame.

See also: abssign_intmin_intmax_intint_floor